using System;
using System.Collections.Generic;
using System.Reflection.Emit;
using HarmonyLib;

namespace TheWorldOfKongfuEasyMod;

[HarmonyPatch(typeof(CreateWGController ))]
[HarmonyPatch("InitPlayer")]
public static class Patch_CreateWGController_StarLimit
{
    public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
    {
        var codeMatcher = new CodeMatcher(instructions, generator);

        if (!Main.Setting._007isCreateWuGong10StarLimit.Value)
            return instructions;

        codeMatcher.MatchForward(false, new CodeMatch(OpCodes.Ldc_I4_3)
        );
        if (!codeMatcher.IsValid)
        {
            throw new InvalidOperationException("Can't find PackageController.UpdatePackageItemDetail");
        }

        codeMatcher.RemoveInstruction()
            .InsertAndAdvance(
            new CodeInstruction(OpCodes.Ldc_I4_7)
        );
        return codeMatcher.Instructions();
    }

}